//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 14
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using ProxyHelpers.EWS;

namespace AndFilterWithMultipleChildren
{
    class Program
    {
        static void Main(string[] args)
        {
            // Listing 14-7
            // first child of And --> Not(IsGreaterThan(itemSize, 100000))
            //
            NotType not = new NotType();

            // build up our IsGreaterThan leaf filter
            //
            IsGreaterThanType greaterThan = new IsGreaterThanType();
            PathToUnindexedFieldType sizePath = new PathToUnindexedFieldType();
            sizePath.FieldURI = UnindexedFieldURIType.itemSize;
            greaterThan.Item = sizePath;
            FieldURIOrConstantType constant = new FieldURIOrConstantType();
            ConstantValueType constantValue = new ConstantValueType();
            constantValue.Value = "100000";

            // Note that FieldURIOrConstantType.Item is of type object.  This is 
            // because it can take either a ConstantValueType instance or a property 
            // path such as PathToUnindexedFieldType.
            //
            constant.Item = constantValue;
            greaterThan.FieldURIOrConstant = constant;
            not.Item = greaterThan;

            // second child of And --> IsLessThan(datesent, 2006-11-03T12:00:00Z)

            IsLessThanType lessThan = new IsLessThanType();
            PathToUnindexedFieldType dateSentPath = new PathToUnindexedFieldType();
            dateSentPath.FieldURI = UnindexedFieldURIType.itemDateTimeSent;
            lessThan.Item = dateSentPath;
            FieldURIOrConstantType dateConstant = new FieldURIOrConstantType();
            ConstantValueType dateConstantValue = new ConstantValueType();
            dateConstantValue.Value = "2006-11-03T12:00:00Z";

            dateConstant.Item = dateConstantValue;
            lessThan.FieldURIOrConstant = dateConstant;

            // last child of And --> IsEqualTo(itemClass, "IPM.Note")
            //
            IsEqualToType isEqualTo = new IsEqualToType();
            PathToUnindexedFieldType itemClassPath = new PathToUnindexedFieldType();
            itemClassPath.FieldURI = UnindexedFieldURIType.itemItemClass;
            isEqualTo.Item = itemClassPath;
            FieldURIOrConstantType itemClassConstant = new FieldURIOrConstantType();
            ConstantValueType itemClassConstantValue = new ConstantValueType();
            itemClassConstantValue.Value = "IPM.Note";

            itemClassConstant.Item = itemClassConstantValue;
            isEqualTo.FieldURIOrConstant = itemClassConstant;

            // now, create our and.  It has three child filters
            //
            AndType and = new AndType();
            and.Items = new SearchExpressionType[] { greaterThan, lessThan, isEqualTo };

            RestrictionType restriction = new RestrictionType();
            // Set our And instance to be the filter associated with our restriction 
            // element

            restriction.Item = and;
        }
    }
}
